package com.umeox.watch.calendar;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;
import com.umeox.moto.watch.themes.skin.loader.ResourceManager;

import java.util.Locale;

/**
 * Created by Rambo 2022/10/8
 * Describe:
 */
public class SimpleMonthView extends MonthView {

    private int mRadius;
    private int mPadding;
    private final Typeface mTypeface;

    /**
     * 周末日期背景画笔
     */
    private final Paint mSolarTermTextPaint = new Paint();

    public SimpleMonthView(Context context) {
        super(context);
        mPadding = dipToPx(getContext(), 1);
        AssetManager mgr = context.getAssets();
        mTypeface = Typeface.createFromAsset(mgr, "font/NUNITO-REGULAR.TTF");
        setLayoutDirection(LAYOUT_DIRECTION_LOCALE);
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
        mSchemePaint.setStyle(Paint.Style.STROKE);

        mSolarTermTextPaint.setColor(ResourceManager.getInstance().getColor(R.color.color_3316af6));
        mSolarTermTextPaint.setAntiAlias(true);
        mSolarTermTextPaint.setStyle(Paint.Style.FILL);
        mSolarTermTextPaint.setStrokeWidth(2);

        mCurMonthTextPaint.setColor(ResourceManager.getInstance().getColor(R.color.color_ac80ff));
        mSelectedPaint.setColor(ResourceManager.getInstance().getColor(R.color.color_663db3));
        mCurDayTextPaint.setColor(ResourceManager.getInstance().getColor(R.color.color_ac80ff));
        mOtherMonthTextPaint.setColor(ResourceManager.getInstance().getColor(R.color.color_other_month_text_color));
    }

    @Override
    protected void onLoopStart(int x, int y) {

    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {

        if (!calendar.isWeekend()) {
//            if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
//                canvas.drawRect(162 - x + mPadding, y + mPadding, 162 - x + mItemWidth - mPadding, y + mItemHeight - mPadding, mSelectedPaint);
//            } else {
            canvas.drawRect(x + mPadding, y + mPadding, x + mItemWidth - mPadding, y + mItemHeight - mPadding, mSelectedPaint);
//            }

        } else {
            mSolarTermTextPaint.setColor(mSelectedPaint.getColor());
//            if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
//                canvas.drawRect(162 - x + mPadding, y + mPadding, 162 - x + mItemWidth - mPadding, y + mItemHeight - mPadding, mSolarTermTextPaint);
//            } else {
            canvas.drawRect(x + mPadding, y + mPadding, x + mItemWidth - mPadding, y + mItemHeight - mPadding, mSolarTermTextPaint);
//            }
            mSolarTermTextPaint.setColor(ResourceManager.getInstance().getColor(R.color.color_3316af6));
        }

        return false;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        canvas.drawCircle(cx, cy, mRadius, mSchemePaint);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx;
//        if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL && !isSelected) {
//            cx = 162 - x + mItemWidth / 2;
//        } else {
            cx = x + mItemWidth / 2;
//        }

//        Logger.e("isSelected=" + isSelected + ",cx=" + cx);

        if (calendar.isWeekend() && !calendar.isCurrentDay() && !isSelected && calendar.isCurrentMonth()) {
//            if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
//                canvas.drawRect(162 - x + mPadding, y + mPadding, 162 - x + mItemWidth - mPadding, y + mItemHeight - mPadding, mSolarTermTextPaint);
//            } else {
                canvas.drawRect(x + mPadding, y + mPadding, x + mItemWidth - mPadding, y + mItemHeight - mPadding, mSolarTermTextPaint);
//            }
        }

        if (isSelected) {
            String day = String.format(Locale.getDefault(), "%d", calendar.getDay());
//            String day = String.valueOf(calendar.getDay());
//            if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
//                Logger.e("calendar.getWeek()=" + calendar.getWeek() + ",calendar.getDay()=" + calendar.getDay() + "，cx=" + cx);
//                if (calendar.getWeek() == 1) {
//                    day = String.format(Locale.getDefault(), "%d", calendar.getDay() + 6);
//                } else if (calendar.getWeek() == 2) {
//                    day = String.format(Locale.getDefault(), "%d", calendar.getDay() + 4);
//                } else if (calendar.getWeek() == 3) {
//                    day = String.format(Locale.getDefault(), "%d", calendar.getDay() + 2);
//                } else if (calendar.getWeek() == 5) {
//                    day = String.format(Locale.getDefault(), "%d", calendar.getDay() - 2);
//                } else if (calendar.getWeek() == 6) {
//                    day = String.format(Locale.getDefault(), "%d", calendar.getDay() - 4);
//                } else if (calendar.getWeek() == 0) {
//                    day = String.format(Locale.getDefault(), "%d", calendar.getDay() - 6);
//                }
//
//                mSelectTextPaint.setColor(Color.WHITE);
//            }
            canvas.drawText(day,
                    cx,
                    baselineY,
                    mSelectTextPaint);
        } else if (hasScheme) {
            String day = String.format(Locale.getDefault(), "%d", calendar.getDay());
            canvas.drawText(day,
                    cx,
                    baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);

        } else {
            String day = String.format(Locale.getDefault(), "%d", calendar.getDay());
//            String day = String.valueOf(calendar.getDay());
            canvas.drawText(day, cx, baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }

    private int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
